//
//  LeftViewController.m
//  WealthManager
//
//  Created by rimi on 15/9/28.
//  Copyright (c) 2015年 魏攀. All rights reserved.
//

#import "LeftViewController.h"
#import "CustomTableViewCell.h"
#import "MainViewController.h"

static NSString *const kUITableViewCellIdentifier = @"cellIdentifier";

@interface LeftViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) NSMutableArray * dataSourceBefore;/**< 登录前的按钮 */

@property (nonatomic, strong) NSArray * imageArrBefore;/**< 登录前的按钮图片数组 */
@property (nonatomic, strong) NSMutableArray * dataSourceAfter;/**< 登录后的按钮 */

@property (nonatomic, strong) NSArray * imageArrAfter;/**< 登录后的按钮数组 */
@property (nonatomic, assign) BOOL isBefore;/**< 登录前 */

@property (nonatomic, strong) UITableView * tableView;/**< tab按钮 */
@property (nonatomic, strong) MainViewController * mainVC;
@property (nonatomic, strong) UIButton * scrollBtn;/**< 滑动开关 */
@property (nonatomic, strong) UILabel * verLabel;/**< 版本文本 */
@property (nonatomic, strong) NSMutableArray * VCArrayBefore;
@property (nonatomic, strong) NSMutableArray * VCArrayAfter;

- (void)initializeUserInterface; /**< 初始化用户界面 */

@end

@implementation LeftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
}
#pragma mark - private methods
- (void)initializeUserInterface {
    self.view.backgroundColor = COLOR_RGB(104, 70, 21, 1);
    self.isBefore = NO;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.scrollBtn];
    [self.view addSubview:self.verLabel];
    [self addChildViewController:self.mainVC];
    
}
#pragma mark - responds events
- (void)anction_onBtn {
    if (self.mainVC.isOpen) {
        [self.mainVC closeLeftViewController];
        return;
    }
    [self.mainVC openLeftViewController];
}
#pragma mark - override
- (void)addChildViewController:(UIViewController *)childController
{
    [super addChildViewController:childController];
    [childController didMoveToParentViewController:childController];
    if (_mainVC.isOpen) {
        self.mainVC.view.frame = DHFlexibleFrame(CGRectMake(225, 0, ORIGIN_WIDTH - 225, ORIGIN_HEIGHT-20),NO);
    } else {
        self.mainVC.view.frame = DHFlexibleFrame(CGRectMake(60, 0, ORIGIN_WIDTH - 60, ORIGIN_HEIGHT-20),NO);
    }
    [self.view addSubview:childController.view];
}

#pragma mark - 协议
#pragma mark---协议UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.isBefore ? self.dataSourceBefore.count:self.dataSourceAfter.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.isBefore ?[self.dataSourceBefore[section] count]:[self.dataSourceAfter[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CustomTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kUITableViewCellIdentifier];
    if (!cell) {
        cell = [[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kUITableViewCellIdentifier];
    }
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    if (self.isBefore) {
        cell.titleImage.image = IMAGE_CONTENT(self.imageArrBefore[indexPath.section][indexPath.row]);
        cell.nameImage.image = IMAGE_CONTENT(self.dataSourceBefore[indexPath.section][indexPath.row]);
    } else {
        cell.titleImage.image = IMAGE_CONTENT(self.imageArrAfter[indexPath.section][indexPath.row]);
        cell.nameImage.image = IMAGE_CONTENT(self.dataSourceAfter[indexPath.section][indexPath.row]);
    }
    
    return cell;
}

#pragma mark---, UITableViewDataSource
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section {
    return section == 0 ? 60 : 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return self.isBefore ? [self.dataSourceBefore[section] count]*70 : 45;
}
//设置每一行高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView * footview = [[UIView alloc]init];
    return footview;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headView = [[UIView alloc]init];
    if (section == 0) {
        UIImageView * headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 45, 50) adjustWidth:NO];
        headImageView.image = IMAGE_CONTENT(@"头像.png");
        headImageView.backgroundColor = COLOR_RGB(104, 70, 21, 1);
        [headView addSubview:headImageView];
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(80, 15, 120, 40) adjustWidth:NO];
        label.text = @"尊敬的用户，";
        [headView addSubview:label];
    }
    return headView;
}
//用户点击了某一行
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"user click cell in row %ld, section %ld", indexPath.row, indexPath.section);
    if (indexPath.section == 0) {
        [self.mainVC.view removeFromSuperview];
        [self.mainVC removeFromParentViewController];
        self.mainVC = nil;
        if (self.isBefore) {
            Class class = NSClassFromString(self.VCArrayBefore[indexPath.row]);
            self.mainVC = [[class alloc]init];
        } else {
            Class class = NSClassFromString(self.VCArrayAfter[indexPath.row]);
            self.mainVC = [[class alloc]init];
            self.mainVC.isOpen = [[NSUserDefaults standardUserDefaults]boolForKey:@"isOpen"];
        }
        [self addChildViewController:_mainVC];
    }
    if (indexPath.section == 1) {

        [self.tableView deselectRowAtIndexPath:indexPath animated:YES];/**< 取消选中 */
    }
}

#pragma mark---getter
- (NSMutableArray *)dataSourceBefore {
    if (!_dataSourceBefore) {
        _dataSourceBefore = [NSMutableArray arrayWithArray:@[@[@"首页.png", @"金融资讯.png", @"理财服务.png", @"银行卡.png", @"代理产品.png", @"我的网银.png"], @[@"登录.png"]]];
    }
    return _dataSourceBefore;
}
- (NSArray *)imageArrBefore {
    if (!_imageArrBefore) {
        _imageArrBefore = @[@[@"首页-1.png", @"金融资讯-1.png", @"理财服务-1.png", @"银行卡-1.png", @"代理产品-1.png", @"我的网银-1.png"], @[@"登录-1.png"]];
    }
    return _imageArrBefore;
}
- (NSMutableArray *)dataSourceAfter {
    if (!_dataSourceAfter) {
        _dataSourceAfter = [NSMutableArray arrayWithArray:@[@[@"首页.png", @"金融资讯.png", @"理财服务.png", @"银行卡.png", @"缴费服务.png", @"代理产品.png", @"我的网银.png", @"活动交流.png", @"日程提醒.png", @"业务咨询.png"], @[@"设置.png", @"登出.png"]]];
    }
    return _dataSourceAfter;
}
- (NSArray *)imageArrAfter {
    if (!_imageArrAfter) {
        _imageArrAfter = @[@[@"首页-1.png", @"金融资讯-1.png", @"理财服务-1.png", @"银行卡-1.png", @"缴费服务-1.png", @"代理产品-1.png", @"我的网银-1.png", @"活动交流-1.png", @"日程提醒-1.png", @"业务咨询-1.png"], @[@"设置-1.png", @"登出-1.png"]];
    }
    return _imageArrAfter;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 10, 300, ORIGIN_HEIGHT-70) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        _tableView.backgroundColor = COLOR_RGB(104, 70, 21, 1);
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}
- (MainViewController *)mainVC {
    if (!_mainVC) {
        Class class = NSClassFromString(self.VCArrayBefore[0]);
        _mainVC = [[class alloc]init];
    }
    return _mainVC;
}
- (UIButton *)scrollBtn {
    if (!_scrollBtn) {
        _scrollBtn = ({
            UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
            btn.frame = DHFlexibleFrame(CGRectMake(0, 945, 70, 70), NO);
            [btn setImage:IMAGE_CONTENT(@"滑动-1.png") forState:UIControlStateNormal];
            [btn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
            [btn addTarget:self action:@selector(anction_onBtn) forControlEvents:UIControlEventTouchUpInside];
            btn;
        });
    }
    return _scrollBtn;
}
- (UILabel *)verLabel {
    if (!_verLabel) {
        _verLabel = ({
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(70, 960, 130, 40) adjustWidth:NO];
            label.text = @"Ver:1.1.0";
            label.textColor = [UIColor whiteColor];
            label;
        });
    }
    return _verLabel;
}
- (NSMutableArray *)VCArrayBefore {
    if (!_VCArrayBefore) {
        _VCArrayBefore = [NSMutableArray arrayWithArray:@[@"HomePageViewController", @"FinanceConsultViewController", @"MoneyServesViewController", @"BankCardViewController",  @"DelegateProductViewController", @"MyNetBankViewController", @"ActivityExchangeViewController",
                                                    ]];
    }
    return _VCArrayBefore;
}
- (NSMutableArray *)VCArrayAfter {
    if (!_VCArrayAfter) {
        _VCArrayAfter = [NSMutableArray arrayWithArray:@[@"HomePageViewController", @"FinanceConsultViewController", @"MoneyServesViewController", @"BankCardViewController", @"MoneyDealServeViewController",  @"DelegateProductViewController", @"MyNetBankViewController", @"ActivityExchangeViewController", @"ScheduleTopViewController", @"BusinessConsultViewController"
                                                          ]];
    }
    return _VCArrayAfter;
}
@end
